package com.mopon.test.tx.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.mopon.test.tx.dao.AccountDao;
import com.mopon.test.tx.domain.Account;
import com.mopon.test.tx.util.TransactionManager;

public class AccountDaoImpl implements AccountDao {

	private QueryRunner qr = new QueryRunner();

    public Account findByName(String accountName) {
        try {
        	Connection conn = TransactionManager.getConnection();
        	System.out.println("Connection: " + conn + "\n" + conn.hashCode());
            return qr.query(conn,"select * from account where name=?", new BeanHandler<Account>(Account.class),accountName);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void updateAcount(Account account) {
        try {
        	Connection conn = TransactionManager.getConnection();
        	System.out.println("Connection: " + conn + "\n" + conn.hashCode());
            qr.update(conn,"update account set money=? where id=?", account.getMoney(),account.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
